package main

import (
	"after_single/src"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

func main() {
	r := gin.Default()
	r.SetTrustedProxies([]string{})

	// 委托追单
	r.POST("/order", func(ctx *gin.Context) {
		var order src.Order
		err := ctx.Bind(&order)
		if err != nil {
			ctx.String(http.StatusOK, err.Error())
			return
		}
		val := validator.New()
		err = val.Struct(&order)
		if err != nil {
			ctx.String(http.StatusOK, err.Error())
			return
		}
		err = src.StartOrder(&order)
		if err != nil {
			ctx.String(http.StatusOK, err.Error())
			return
		}
		ctx.String(http.StatusOK, "OK")
	})
	// 修改配置
	r.POST("/config", func(ctx *gin.Context) {
		var cfg src.TailConfig
		err := ctx.Bind(&cfg)
		if err != nil {
			ctx.String(http.StatusOK, err.Error())
			return
		}
		src.SetConfig(cfg)
		ctx.String(http.StatusOK, "OK")
	})

	go src.Run() // 启动交易接口
	r.Run(":80")
}
